package com.cennavi.minemap.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

import com.cennavi.minemap.bean.RoleBean;
import com.cennavi.minemap.bean.UserBean;
import com.cennavi.minemap.dao.UserDao;
import com.cennavi.minemap.util.DateTimeUtil;
import com.cennavi.minemap.util.StringUtil;

@Service
@ComponentScan(basePackages = { "com.cennavi.minemap.dao" })
public class UserService {
	@Autowired
	private UserDao userDao;

	public List<UserBean> login(UserBean userBean) {
		List<UserBean> returnUser = new ArrayList<UserBean>();
		if (userBean.getUsername() != null && !userBean.getUsername().equals("")) {
			returnUser = userDao.login(userBean.getUsername(), StringUtil.encoderByMd5(userBean.getPassword()),
					"username");
		}
		if (userBean.getMail() != null && !userBean.getMail().equals("")) {
			returnUser = userDao.login(userBean.getMail(), StringUtil.encoderByMd5(userBean.getPassword()), "mail");
		}
		return returnUser;
	}

	public List<UserBean> tencentLogin(String username) {
		return userDao.queryByUsername(username);
	}

	public List<UserBean> register(UserBean userBean) {
		userBean.setPassword(StringUtil.encoderByMd5(userBean.getPassword()));
		userBean.setCreateTime(DateTimeUtil.getNow());
		int id = userDao.insert(userBean);
		userDao.insertUserRole(id);
		return userDao.queryById(id);
	}

	public List<UserBean> update(UserBean userBean) {
		int status = userDao.update(userBean);
		if (status < 0) {
			return null;
		} else {
			return userDao.queryById(userBean.getId());
		}
	}

	public List<UserBean> queryById(int id) {
		return userDao.queryById(id);
	}

	public int queryByMail(String mail) {
		List<UserBean> userBean = userDao.queryByMail(mail);
		return userBean.size();
	}

	public List<UserBean> queryByName(String username) {
		return userDao.queryByUsername(username);
	}

	public int uniqueVerify(String key, String value) {
		return userDao.uniqueVerify(key, value);
	}

	public int updateVerification(String mail, String vCode, String vDate) {
		return userDao.updateVerification(mail, vCode, vDate);
	}

	public List<UserBean> queryByVCode(String mail, String vCode, String now) {
		return userDao.queryByVCode(mail, vCode, now);
	}

	public List<RoleBean> queryRoleByUserId(Integer userId) {
		return userDao.queryRoleByUserId(userId);
	}

	public List<UserBean> updatePassword(UserBean userBean) {
		userBean.setPassword(StringUtil.encoderByMd5(userBean.getPassword()));
		int status = userDao.updatePassword(userBean);
		if (status < 0) {
			return null;
		} else {
			return userDao.queryById(userBean.getId());
		}
	}

	public List<UserBean> mailboxVeri(UserBean userBean) {
		int status = userDao.mailboxVeri(userBean);
		if (status < 0) {
			return null;
		} else {
			return userDao.queryById(userBean.getId());
		}
	}
}
